package cn.qihua.action;

import java.sql.Timestamp;

import org.apache.struts2.json.annotations.JSON;

import cn.qihua.model.BaseOrder;
import cn.qihua.service.IOrderService;

import com.opensymphony.xwork2.ActionSupport;

/**
 * @author 米修
 * 
 */

public class OrderAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 3304239783129613128L;
	public String message;
	private boolean success = false;
	private int id;
	private int uid;
	private String Status;
	private String siteUrl;
	private IOrderService baseOrderService;
	private BaseOrder order = new BaseOrder();

	public String findById() {
		this.order = baseOrderService.findById(id);
		if (order == null) {
			this.setSuccess(false);
			this.setMessage("加载失败！");
			return SUCCESS;
		} 
		this.setSuccess(true);
		return SUCCESS;
	}
	public String addOrder() {
		if (order != null) {
			order.setCreatedate(new Timestamp(System.currentTimeMillis()));
			order.setIsnew(true);
			order.setStatus("新订单");
			if (baseOrderService.add(order)) {
				this.setSuccess(true);
				this.setMessage(order.getText() + " 添加成功！");
				return SUCCESS;
			}
		}
		this.setSuccess(false);
		this.setMessage("添加失败！");
		return ERROR;
	}

	public String editOrder() {
		if (order != null) {
			baseOrderService.edit(order);
			this.setSuccess(true);
			this.setMessage("修改成功！");
			return SUCCESS;
		} else {
			this.setSuccess(false);
			this.setMessage("修改失败！");
			return ERROR;
		}
	}

	public String changeOrderSta() {
		if (baseOrderService.changeStatus(id, Status)) {
			this.setSuccess(true);
			this.setMessage("修改成功！");
			return SUCCESS;
		} else {
			this.setSuccess(false);
			this.setMessage("修改失败！");
			return ERROR;
		}
	}

	public String delOrder() {
		if (baseOrderService.del(id)) {
			this.setSuccess(true);
			this.setMessage("删除成功！");
			return SUCCESS;
		} else {
			this.setSuccess(false);
			this.setMessage("删除失败！");
			return ERROR;
		}
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getUid() {
		return uid;
	}

	public void setUid(int uid) {
		this.uid = uid;
	}

	public String getStatus() {
		return Status;
	}

	public void setStatus(String status) {
		Status = status;
	}

	public String getSiteUrl() {
		return siteUrl;
	}

	public void setSiteUrl(String siteUrl) {
		this.siteUrl = siteUrl;
	}
	@JSON(name = "data")
	public BaseOrder getOrder() {
		return order;
	}

	public void setOrder(BaseOrder order) {
		this.order = order;
	}

	public void setBaseOrderService(IOrderService baseOrderService) {
		this.baseOrderService = baseOrderService;
	}

}
